package com.jeesite.modules.app.examinate.extheory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.Application;
import com.jeesite.modules.app.examinate.extheory.entity.AppExaminationBarbecue;
import com.jeesite.modules.app.examinate.extheory.service.AppExaminationBarbecueService;
import com.jeesite.modules.app.user.entity.AppUser;
import com.jeesite.modules.app.user.service.AppUserService;
import com.jeesite.modules.app.util.wx.IdCardVerifyUtil;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.zx.barbecue.signup.entity.TbarbecueSignup;
import com.jeesite.modules.zx.barbecue.signup.service.TbarbecueSignupService;
import com.jeesite.modules.zx.examination.barbecue.record.entity.TexaminationBarbecueRecord;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/app/barbecue/signup/")
public class AppSignupController {
	@Autowired
	private TbarbecueSignupService tbarbecueSignupService;
	@Autowired
	private AppExaminationBarbecueService appExaminationBarbecueService;
	@Autowired
	private AppUserService appUserService;
	 @ModelAttribute()  
	  public void alideUser(HttpServletRequest request,HttpServletResponse response){
		 String userId= request.getParameter("userId");
		 if(StringUtils.isBlank(userId)){
				AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
				return;
			}
		 String curSessionKey=request.getParameter("sessionKey");
		 if(StringUtils.isBlank(curSessionKey)){
			 AppController.renderErrorString(response, MessageCode.NOLOGIN, "或登录信息异常");
			 return;
		 }
		 AppUser appUser = appUserService.get(userId);
		 if(appUser!=null){
			 String sessionKey = appUser.getSessionKey();
			 if(StringUtils.isNotBlank(sessionKey)){
		       if(!sessionKey.equals(curSessionKey)){
		    	   AppController.renderErrorString(response, MessageCode.RELOGIN, "");
		    	   return;
		       }
			 }
		 }
//		 Banner.Mode.LOG
		 System.out.println(userId);
	  }

	@RequestMapping(value="checkSignUp")
	public void checkSignUp(@RequestParam(value = "userId",required=true) String userId,
					  @RequestParam(value = "barbecueId",required=true) String barbecueId
			  , HttpServletRequest request, HttpServletResponse response){
		TbarbecueSignup signup=new TbarbecueSignup();
		signup.setBarbecueId(barbecueId);
		signup.setUserId(userId);
		signup.getSqlMap().getWhere().and("state", QueryType.NE, "2");
		Long findCount = tbarbecueSignupService.findCount(signup);
		if(findCount!=null&&findCount>0){
			AppController.renderErrorString(response, MessageCode.ERROR_MESSAGE, "已经报考过该项目");
			return;
		}
		AppController.renderSuccessObject(response, null, MessageCode.SUCCESS_ACK);
	}


	@RequestMapping(value="add")
	public void add(TbarbecueSignup entity,HttpServletRequest request,HttpServletResponse response){
		String barbecueId = entity.getBarbecueId();
		if(StringUtils.isBlank(barbecueId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "主题标识");
			return;
		}
		String idCard = entity.getIdCard();
		if(StringUtils.isBlank(idCard)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "身份证号");
			return;
		}
		
		IdCardVerifyUtil idv=new IdCardVerifyUtil();
		if(!idv.verify(idCard)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "身份证编号格式不正确");
			return;
		}
		String username=entity.getUsername();
		if(StringUtils.isBlank(username)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "考生姓名");
			return;
		}
		String mobile=entity.getMobile();
		if(StringUtils.isBlank(mobile)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "手机号");
			return;
		}
		if(!validateMobile(mobile)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "手机号格式异常");
			return;
		}
		String userPhoto=entity.getUserPhoto();
		if(StringUtils.isBlank(userPhoto)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "考生头像");
			return;
		}
		String userId=request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		TbarbecueSignup signup=new TbarbecueSignup();
		signup.setBarbecueId(barbecueId);
		signup.setUserId(userId);
		
		signup.getSqlMap().getWhere().and("state", QueryType.NE, "2");
		Long findCount = tbarbecueSignupService.findCount(signup);
		if(findCount!=null&&findCount>0){
			AppController.renderErrorString(response, MessageCode.ERROR_MESSAGE, "已经报考过该项目");
			return;
		}else{
			entity.setCreateBy(userId);
			entity.setState(0);
			tbarbecueSignupService.save(entity);
		}
		JSONObject json=JSONObject.parseObject(JsonMapper.toJson(entity));
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}
	private void updateEmp(String idCard){
	}
	private boolean validateMobile(String mobile){
		String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
		if(mobile.length() != 11){
			return false;
		}else{
			Pattern p=Pattern.compile(regex);
			Matcher m=p.matcher(mobile);
			boolean isMatch=m.matches();
			if(isMatch){
				return true;
			}else{
				return false;
			}
		}
	}
	@RequestMapping(value ="update")
	public void update(TbarbecueSignup entity, HttpServletRequest request,HttpServletResponse response){
		String id=entity.getId();
		if(StringUtils.isBlank(id)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "标识不存在");
			return;
		}
		entity.setBarbecueTime(new Date());
		entity.setState(3);
		tbarbecueSignupService.update(entity);
		AppController.renderSuccessObject(response, "", MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value ="findPage")
	public void findPage(TbarbecueSignup entity, HttpServletRequest request,HttpServletResponse response){
		String userId=request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		entity.setCreateBy(userId);
		entity.setPage(new Page<>(request, response));
		Page<TbarbecueSignup> findPage = tbarbecueSignupService.findPage(entity);
		JSONObject json=JSONObject.parseObject(JsonMapper.toJson(findPage));
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}
}
